package com.yikaiye.android.yikaiye.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.DataBindingHolder;
import com.yikaiye.android.yikaiye.R;
import com.yikaiye.android.yikaiye.databinding.ItemServiceProgressBinding;
import com.yikaiye.android.yikaiye.databinding.ItemServiceProgressLayoutBinding;
import com.yikaiye.android.yikaiye.model.ServiceDetailsModel;
import com.yikaiye.android.yikaiye.model.ServiceRecordsModel;

public class ServiceProgressAdapter extends BaseQuickAdapter<ServiceDetailsModel.ServiceProgressModel, DataBindingHolder<ItemServiceProgressLayoutBinding>> {

    private final Context context;

    public ServiceProgressAdapter(Context context) {
        this.context = context;
    }

    @Override
    protected void onBindViewHolder(@NonNull DataBindingHolder<ItemServiceProgressLayoutBinding> itemMyServiceBindingDataBindingHolder, int i, @Nullable ServiceDetailsModel.ServiceProgressModel serviceProgressModel) {
        ItemServiceProgressLayoutBinding binding = itemMyServiceBindingDataBindingHolder.getBinding();
        if (serviceProgressModel != null) {
            if (serviceProgressModel.getPeriod() != null) {
                binding.tvCurrentTerm.setText(context.getString(R.string.service_node_cycle2, serviceProgressModel.getPeriod()));
            }
            ServiceNodeAdapter nodeAdapter = new ServiceNodeAdapter(context);
            binding.rv.setAdapter(nodeAdapter);
            nodeAdapter.submitList(serviceProgressModel.getServiceNodeList());
        }
    }

    @NonNull
    @Override
    protected DataBindingHolder<ItemServiceProgressLayoutBinding> onCreateViewHolder(@NonNull Context context, @NonNull ViewGroup viewGroup, int i) {
        return new DataBindingHolder<>(R.layout.item_service_progress_layout, viewGroup);
    }

    public static class ServiceNodeAdapter extends BaseQuickAdapter<ServiceDetailsModel.ServiceNodeListModel, DataBindingHolder<ItemServiceProgressBinding>> {

        private final Context context;

        public ServiceNodeAdapter(Context context) {
            this.context = context;
        }

        @Override
        protected void onBindViewHolder(@NonNull DataBindingHolder<ItemServiceProgressBinding> itemMyServiceBindingDataBindingHolder, int i, @Nullable ServiceDetailsModel.ServiceNodeListModel serviceNodeListModel) {
            ItemServiceProgressBinding binding = itemMyServiceBindingDataBindingHolder.getBinding();
            if (serviceNodeListModel != null) {
                // 节点状态：0.待服务 1.服务中 2.已完成
                if (serviceNodeListModel.getStatus() != null) {
                    if (serviceNodeListModel.getStatus() == 0) {
                        binding.serviceStatusTv.setText("待服务");
                        binding.serviceStatusTv.setTextColor(ContextCompat.getColor(context, R.color.color_3b3a40));
                    } else if (serviceNodeListModel.getStatus() == 1) {
                        binding.serviceStatusTv.setText("服务中");
                        binding.serviceStatusTv.setTextColor(ContextCompat.getColor(context, R.color.color_f93f50));
                    } else if (serviceNodeListModel.getStatus() == 2) {
                        binding.serviceStatusTv.setText("已完成");
                        binding.serviceStatusTv.setTextColor(ContextCompat.getColor(context, R.color.color_f93f50));
                    }
                }
                if (getItemCount() == 3) {
                    if (i == 0) {

                    }
                }

                binding.line.setVisibility(i == getItemCount() - 1 ? View.GONE : View.VISIBLE);
            }
        }

        @NonNull
        @Override
        protected DataBindingHolder<ItemServiceProgressBinding> onCreateViewHolder(@NonNull Context context, @NonNull ViewGroup viewGroup, int i) {
            return new DataBindingHolder<>(R.layout.item_service_progress, viewGroup);
        }
    }
}
